/* Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.*/
(function(a){function b(a){return a&&a.Object===Object?a:null}var c={"function":!0,object:!0},d=c[typeof exports]&&exports&&!exports.nodeType?exports:null,e=c[typeof module]&&module&&!module.nodeType?module:null,f=b(d&&e&&"object"==typeof global&&global),g=b(c[typeof self]&&self),h=b(c[typeof window]&&window),i=(e&&e.exports===d?d:null,b(c[typeof this]&&this)),j=f||h!==(i&&i.window)&&h||g||i||Function("return this")();"function"==typeof define&&define.amd?define(["./rx"],function(b,c){return a(j,c,b)}):"object"==typeof module&&module&&module.exports===d?module.exports=a(j,module.exports,require("./rx")):j.Rx=a(j,{},j.Rx)}).call(this,function(a,b,c,d){function e(a){return function(){try{return a.apply(this,arguments)}catch(b){return w.e=b,w}}}function f(a){this.patterns=a}function g(a,b){this.expression=a,this.selector=b}function h(a){return function(b){a.onError(b)}}function i(a,b){return function(){var c=x(a.selector).apply(a,arguments);return c===w?b.onError(c.e):void b.onNext(c)}}function j(a,b,c){var d=a.get(b);if(!d){var e=new z(b,c);return a.set(b,e),e}return d}function k(a,b,c){this.joinObserverArray=a,this.onNext=b,this.onCompleted=c,this.joinObservers=new y;for(var d=0,e=this.joinObserverArray.length;e>d;d++){var f=this.joinObserverArray[d];this.joinObservers.set(f,f)}}var l=c.Observable,m=l.prototype,n=c.AnonymousObservable,o=l.throwError,p=c.Observer.create,q=c.SingleAssignmentDisposable,r=c.CompositeDisposable,s=c.internals.AbstractObserver,t=c.helpers.noop,u=c.internals.inherits,v=c.helpers.isFunction,w={e:{}},x=c.internals.tryCatch=function(a){if(!v(a))throw new TypeError("fn must be a function");return e(a)},y=a.Map||function(){function a(){this.size=0,this._values=[],this._keys=[]}return a.prototype["delete"]=function(a){var b=this._keys.indexOf(a);return-1===b?!1:(this._values.splice(b,1),this._keys.splice(b,1),this.size--,!0)},a.prototype.get=function(a){var b=this._keys.indexOf(a);return-1===b?d:this._values[b]},a.prototype.set=function(a,b){var c=this._keys.indexOf(a);return-1===c?(this._keys.push(a),this._values.push(b),this.size++):this._values[c]=b,this},a.prototype.forEach=function(a,b){for(var c=0;c<this.size;c++)a.call(b,this._values[c],this._keys[c])},a}();f.prototype.and=function(a){return new f(this.patterns.concat(a))},f.prototype.thenDo=function(a){return new g(this,a)},g.prototype.activate=function(a,b,c){for(var d=[],e=h(b),f=0,g=this.expression.patterns.length;g>f;f++)d.push(j(a,this.expression.patterns[f],e));var l=new k(d,i(this,b),function(){for(var a=0,b=d.length;b>a;a++)d[a].removeActivePlan(l);c(l)});for(f=0,g=d.length;g>f;f++)d[f].addActivePlan(l);return l},k.prototype.dequeue=function(){this.joinObservers.forEach(function(a){a.queue.shift()})},k.prototype.match=function(){var a,b,c=!0;for(a=0,b=this.joinObserverArray.length;b>a;a++)if(0===this.joinObserverArray[a].queue.length){c=!1;break}if(c){var d=[],e=!1;for(a=0,b=this.joinObserverArray.length;b>a;a++)d.push(this.joinObserverArray[a].queue[0]),"C"===this.joinObserverArray[a].queue[0].kind&&(e=!0);if(e)this.onCompleted();else{this.dequeue();var f=[];for(a=0,b=d.length;a<d.length;a++)f.push(d[a].value);this.onNext.apply(this,f)}}};var z=function(a){function b(b,c){a.call(this),this.source=b,this.onError=c,this.queue=[],this.activePlans=[],this.subscription=new q,this.isDisposed=!1}u(b,a);var c=b.prototype;return c.next=function(a){if(!this.isDisposed){if("E"===a.kind)return this.onError(a.error);this.queue.push(a);for(var b=this.activePlans.slice(0),c=0,d=b.length;d>c;c++)b[c].match()}},c.error=t,c.completed=t,c.addActivePlan=function(a){this.activePlans.push(a)},c.subscribe=function(){this.subscription.setDisposable(this.source.materialize().subscribe(this))},c.removeActivePlan=function(a){this.activePlans.splice(this.activePlans.indexOf(a),1),0===this.activePlans.length&&this.dispose()},c.dispose=function(){a.prototype.dispose.call(this),this.isDisposed||(this.isDisposed=!0,this.subscription.dispose())},b}(s);return m.and=function(a){return new f([this,a])},m.thenDo=function(a){return new f([this]).thenDo(a)},l.when=function(){var a,b=arguments.length;if(Array.isArray(arguments[0]))a=arguments[0];else{a=new Array(b);for(var c=0;b>c;c++)a[c]=arguments[c]}return new n(function(b){var c=[],d=new y,e=p(function(a){b.onNext(a)},function(a){d.forEach(function(b){b.onError(a)}),b.onError(a)},function(a){b.onCompleted()});try{for(var f=0,g=a.length;g>f;f++)c.push(a[f].activate(d,e,function(a){var d=c.indexOf(a);c.splice(d,1),0===c.length&&b.onCompleted()}))}catch(h){return o(h).subscribe(b)}var i=new r;return d.forEach(function(a){a.subscribe(),i.add(a)}),i})},c});
//# sourceMappingURL=rx.joinpatterns.map